<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mcategories extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getNamecate()
    {
        return $this->db->query("SELECT * FROM categories ORDER BY order_by ASC")->result_object();
    }
    
    
    
    public function getCategories($id)
    {
        $var = $this->db->query('SELECT * FROM categories WHERE id = '.$id)->row_object();
        $exp_tags = explode(',',$var->tags);
        $tags = '';
        foreach($exp_tags AS $el)
        {
            $tags .= '"'.$el.'",';
        }
        $var->tags_show = substr($tags, 0, -1);
        return $var;
    }
    
    
    public function sortsaveDB($id_list)
    {
        foreach($id_list AS $key=>$val)
        {
            $this->db->update('categories', array('order_by' => $key), "id = '".$val."'");   
        }
        return 200;    
    }

     
    public function editCateDb($id,$img,$type,$baseurl)
    {
        $namemenu           = $this->getParamString('nameCate');
        $title_ascii        = $this->get_ascii($namemenu);
        $nameCateSeo        = $this->getParamString('nameCateSeo');
        $nameCateSeo_ascii  = $this->get_ascii($nameCateSeo);
        $tags               = $this->getParamString('hidden-undefined');
        $DesProSeo          = $this->getParamString('DesProSeo');
        $DesProSeo_ascii    = $this->get_ascii($DesProSeo);
        $parent             = $this->getParamInt('parent');
        @$c_type = $this->db->query('SELECT id_type FROM categories WHERE id = '.$parent)->row_object()->id_type;
        
        $data = array(
                    'tags'                      => $tags,
                    'title'                     => $namemenu,
                    'title_ascii'               => $title_ascii,
                    'title_seo'                 => $nameCateSeo,
                    'title_seo_ascii'           => $nameCateSeo_ascii,
                    'description_seo'           => $DesProSeo,
                    'description_seo_ascii'     => $DesProSeo_ascii,
                    'id_type'                   => $type,
                    'parent'                    => $parent
                    );
        
        if(!in_array($img, array(null, '', '0')))
        {
            $data['img'] = $img;
            // X? lý thêm ph?n xóa ?nh
        }
        
        $this->db->update('categories', $data,"id = ".$id."");
        
        if($parent != 0)
        {
            $n_link = $this->db->query('SELECT n_link FROM categories WHERE id = '.$id)->row_object()->n_link;
            if($type == 1)
            {
                $u_p = 'product/';
            }else{
                $u_p = 'content/';
            }
            $link = array(
                    'link' => $u_p.$this->replace_url($title_ascii).'/'.$this->id_encode($id).'.html',
                    'title' => 'Danh mục '.$namemenu,
                    'type_link' => $type,
                    'type_cate' => $c_type
                    );
            $this->db->update('link_all', $link,"n_link = '".$n_link."'");
        }
        
        return 200;
    }
    
    public function addCateDb($img,$type,$baseurl)
    {
        $namemenu           = $this->getParamString('nameCate');
        $title_ascii        = $this->get_ascii($namemenu);
        $nameCateSeo        = $this->getParamString('nameCateSeo');
        $nameCateSeo_ascii  = $this->get_ascii($nameCateSeo);
        $tags               = $this->getParamString('hidden-undefined');
        $DesProSeo          = $this->getParamString('DesProSeo');
        $DesProSeo_ascii    = $this->get_ascii($DesProSeo);
        $parent             = $this->getParamInt('parent');
        @$c_type = $this->db->query('SELECT id_type FROM categories WHERE id = '.$parent)->row_object()->id_type;
        $data = array(
                    'tags'                      => $tags,
                    'title'                     => $namemenu,
                    'title_ascii'               => $title_ascii,
                    'title_seo'                 => $nameCateSeo,
                    'title_seo_ascii'           => $nameCateSeo_ascii,
                    'description_seo'           => $DesProSeo,
                    'description_seo_ascii'     => $DesProSeo_ascii,
                    'img'                       => $img,
                    'id_type'                   => $type,
                    'parent'                    => $parent,
                    'n_link'                    => 'cate_'.$type.'_link'.$title_ascii
                    );
        
        $this->db->insert('categories', $data);
        $id = $this->db->insert_id();
        if($parent != 0)
        {
            if($type == 1)
            {
                $u_p = 'product/';
            }else{
                $u_p = 'content/';
            }
            
            $link = array(
                    'link' => $u_p.$this->replace_url($title_ascii).'/'.$this->id_encode($id).'.html',
                    'title' => 'Danh mục '.$namemenu,
                    'type_link' => $type,
                    'type_cate' => $c_type,
                    'n_link'    => 'cate_'.$type.'_link'.$title_ascii
                    );
            $this->db->insert('link_all', $link);
        }
        
        return 200;
    }
    
    
    public function deletemenuproductDB()
    {
        $id = $this->getParamInt('id');
        $n_link = $this->db->query('SELECT n_link FROM categories WHERE id = '.$id)->row_object()->n_link;
        $this->db->delete('link_all',"n_link = '".$n_link."'");
        $this->db->delete('categories',"id = '".$id."' AND parent != 0");
        return 200;
    }
    
    public function getParent($selected = 0, $id_type = '')
    {
        $html = '';
    	$gRs = $this->db->query("SELECT id AS gid,title AS gtitle,parent AS gparent FROM categories WHERE parent = 0 AND id_type = '".$id_type."' ORDER BY order_by ASC")->result_array();
        if($gRs)
        {
            $child = '|--';
            foreach ($gRs As $gel)
        	{
            	   if($selected == $gel['gid'])
                   {
                        $selecteds = 'selected="selected"';
                   }else{
                        $selecteds = '';
                   }
    	    		$html .= ' <option value="' . $gel['gid'] . '"'.$selecteds.'>'.$gel['gtitle'].'</option>';
                    $html .= $this->subdanhmucGroupotp($selected, $gel['gid'], '',$id_type);
        	}
        }
        return $html;
    }
    private function subdanhmucGroupotp($selected, $id, $child, $id_type)
    {
        $child = $child .'|--';
        $htmls = '';
        $gGs = $this->db->query("SELECT id AS sGid,parent AS sGparent, title AS sGtitle FROM categories WHERE parent = '".$id."' AND id_type = '".$id_type."' ORDER BY id ASC")->result_array();
        foreach ($gGs As $key=>$val)
		{
		      if($selected == $val['sGid'])
               {
                    $selecteds = 'selected="selected"';
               }else{
                    $selecteds = '';
               }
                $htmls .= '<option value="' . $val['sGid'] . '" '.$selecteds;
                $htmls .= ($selected == $val['sGid']) ? 'selected="selected"' : '';
				$htmls .= '>' . $child .' '. $val['sGtitle'] . '</option>
                ';
                $htmls .= $this->subdanhmucGroupotp($selected, $val['sGid'], $child, $id_type);
		}
        return $htmls;
    }
    
    public function getCategoriesDB($baseurl,$id_type,$actions)
    {
        $html = '';
        $data = $this->db->query('SELECT c.title AS ctitle,c.id AS cid, tc.title AS ttitle FROM categories AS c INNER JOIN type_categories AS tc ON tc.id = c.id_type WHERE parent = 0 AND id_type = '.$id_type.' ORDER BY cid,order_by ASC')->result_array();
        if($data)
        {
            $i = 1;
            foreach($data AS $le=>$el)
            {
                $child = '|---- ';
                $html .= '<tr>
                            <td><span class="xedit"><center>'.$i.'</center></span></td>
                            <td class="hidden-xs"><span class="xedit"><strong>'.$el['ctitle'].'</strong></span></td>
                            <td class="hidden-xs"><span class="xedit"><strong>'.$el['ttitle'].'</strong></span></td>
                            <td class="hidden-xs text-center"><div class="btn-group">
                                <button type="button" class="btn btn-danger btn-gradient dropdown-toggle" data-toggle="dropdown"> <span class="glyphicons glyphicons-cogwheel"></span> </button>
                                <ul class="dropdown-menu checkbox-persist pull-right text-left" role="menu">
                                  <li><a href="'.$baseurl.'manage/edit-'.$actions.'.html?id='.$el['cid'].'"><i class="fa"></i> Sửa </a></li>
                                  <li><a href="'.$baseurl.'manage/delete-'.$actions.'.html?id='.$el['cid'].'"><i class="fa"></i> Xóa </a></li>
                                </ul>
                              </div></td>
                          </tr>';
                $i++;
                $html .= $this->subgetdata($el['cid'],$baseurl,$id_type,$child,$i,$actions);
                
            }
        }
        
        return $html;
    }
    
    private function subgetdata($id,$baseurl,$id_type,$child,$i,$actions)
    {
        $html = ''; @$child = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$child;
        $data = $this->db->query('SELECT c.title AS ctitle,c.id AS cid, tc.title AS ttitle FROM categories AS c INNER JOIN type_categories AS tc ON tc.id = c.id_type WHERE parent = "'.$id.'" AND id_type = '.$id_type.' ORDER BY cid,order_by ASC')->result_array();
        if($data)
        {
            foreach($data AS $le=>$el)
            {
                $html .= '<tr>
                            <td><span class="xedit">&nbsp;</span></td>
                            <td class="hidden-xs"><span class="xedit">'.$child.$el['ctitle'].'</span></td>
                            <td class="hidden-xs"><span class="xedit">'.$el['ttitle'].'</span></td>
                            <td class="hidden-xs text-center"><div class="btn-group">
                                <button type="button" class="btn btn-danger btn-gradient dropdown-toggle" data-toggle="dropdown"> <span class="glyphicons glyphicons-cogwheel"></span> </button>
                                <ul class="dropdown-menu checkbox-persist pull-right text-left" role="menu">
                                  <li><a href="'.$baseurl.'manage/edit-'.$actions.'.html?id='.$el['cid'].'"><i class="fa"></i> Sửa </a></li>
                                  <li><a href="'.$baseurl.'manage/delete-'.$actions.'.html?id='.$el['cid'].'"><i class="fa"></i> Xóa </a></li>
                                </ul>
                              </div></td>
                          </tr>';
                $html .= $this->subgetdata($el['cid'],$baseurl,$id_type,$child,$i,$actions);
                
            }
        }
        return $html;
    }
    
    
    
    
}